//
//  JDGoodsDescriptionView.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/23.
//

import UIKit

class JDGoodsDescriptionView: JDBaseView {

    var containerView: UIView?
    var priceLabel: UILabel?
    var saleNumberLabel: UILabel?
    var goodsNameLabel: UILabel?
    var addressInfoLabel: UILabel?
    var brandInfoLabel: UILabel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
    }
    
    func setupUIElements() {
        self.containerView = UIView()
        self.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.equalTo(self).offset(12)
            make.right.equalTo(self).offset(-12)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        })
        self.containerView?.layer.cornerRadius = 8
        self.containerView?.backgroundColor = .white
        
        self.priceLabel = UILabel()
        self.containerView?.addSubview(self.priceLabel!)
        self.priceLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.containerView!).offset(10)
            make.height.equalTo(45)
        })
        self.priceLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 32, weight: .semibold), textColor: color("#FA6161"))
        self.priceLabel?.text = "￥"
//        self.priceLabel?.textColor = color("#FA6161")
        self.priceLabel?.priceString(UIFont.systemFont(ofSize: 14, weight: .semibold), UIFont.systemFont(ofSize: 32, weight: .semibold))
        
        self.saleNumberLabel = UILabel()
        self.containerView?.addSubview(self.saleNumberLabel!)
        self.saleNumberLabel?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.centerY.equalTo(self.priceLabel!)
        })
        self.saleNumberLabel?.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        self.saleNumberLabel?.text = ""
        
        self.goodsNameLabel = UILabel()
        self.containerView?.addSubview(self.goodsNameLabel!)
        self.goodsNameLabel?.snp.makeConstraints({ make in
            make.top.equalTo(self.priceLabel!.snp.bottom).offset(10)
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
//            make.height.equalTo(50)
//            make.bottom.equalTo(self).offset(-10)
        })
        self.goodsNameLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 18, weight: .semibold), textColor: color("#333333"),numberOfLines: 2)
        self.goodsNameLabel?.text = ""
        
        self.addressInfoLabel = UILabel()
        self.containerView?.addSubview(self.addressInfoLabel!)
        self.addressInfoLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.goodsNameLabel!.snp.bottom).offset(12)
            make.height.equalTo(21)
            make.bottom.equalTo(self.containerView!).offset(-11)
        })
        self.addressInfoLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#333333"))
        self.addressInfoLabel?.text = ""
        
        let line = UIView()
        self.containerView?.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(self.addressInfoLabel!.snp.right).offset(10)
            make.centerY.equalTo(self.addressInfoLabel!)
            make.size.equalTo(CGSize(width: 1, height: 13))
        }
        line.backgroundColor = color("#333333")
        
        self.brandInfoLabel = UILabel()
        self.containerView?.addSubview(self.brandInfoLabel!)
        self.brandInfoLabel?.snp.makeConstraints({ make in
            make.left.equalTo(line.snp.right).offset(10)
            make.centerY.equalTo(line)
        })
        self.brandInfoLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#333333"))
        self.brandInfoLabel?.text = ""
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func refreshData(goodsModel: JDGoodsDetailModel?) {
        
        guard let confirmModel = goodsModel else { return }
        
        print("已售:\(confirmModel.soldOutTotal)")
        self.saleNumberLabel?.text = String.init(format: "已售:%zd件", confirmModel.soldOutTotal ?? 0) //"已售：500件"
        self.goodsNameLabel?.text = confirmModel.goodsName
        
        
        self.addressInfoLabel?.text = String.init(format: "产地：%@", confirmModel.origin ?? "")
        self.brandInfoLabel?.text = String.init(format: "品牌：%@", confirmModel.brand ?? "")
        self.priceLabel?.text = String.init(format: "￥%.2f", goodsModel?.salePriceText ?? 0.0)
        self.priceLabel?.priceString(UIFont.systemFont(ofSize: 14, weight: .semibold), UIFont.systemFont(ofSize: 32, weight: .semibold))
    }
    
}
